package cn.iocoder.yudao.module.crossborder.platform.service.platformstore;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.crossborder.platform.controller.admin.platformstore.vo.*;
import cn.iocoder.yudao.module.crossborder.platform.dal.dataobject.platformstore.PlatformStoreDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.crossborder.platform.dal.mysql.platformstore.PlatformStoreMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.crossborder.platform.enums.ErrorCodeConstants.*;

/**
 * 跨境电商店铺管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PlatformStoreServiceImpl implements PlatformStoreService {

    @Resource
    private PlatformStoreMapper platformStoreMapper;

    @Override
    public Long createPlatformStore(PlatformStoreSaveReqVO createReqVO) {
        // 插入
        PlatformStoreDO platformStore = BeanUtils.toBean(createReqVO, PlatformStoreDO.class);
        platformStoreMapper.insert(platformStore);

        // 返回
        return platformStore.getId();
    }

    @Override
    public void updatePlatformStore(PlatformStoreSaveReqVO updateReqVO) {
        // 校验存在
        validatePlatformStoreExists(updateReqVO.getId());
        // 更新
        PlatformStoreDO updateObj = BeanUtils.toBean(updateReqVO, PlatformStoreDO.class);
        platformStoreMapper.updateById(updateObj);
    }

    @Override
    public void deletePlatformStore(Long id) {
        // 校验存在
        validatePlatformStoreExists(id);
        // 删除
        platformStoreMapper.deleteById(id);
    }

    @Override
        public void deletePlatformStoreListByIds(List<Long> ids) {
        // 删除
        platformStoreMapper.deleteByIds(ids);
        }


    private void validatePlatformStoreExists(Long id) {
        if (platformStoreMapper.selectById(id) == null) {
            throw exception(PLATFORM_STORE_NOT_EXISTS);
        }
    }

    @Override
    public PlatformStoreDO getPlatformStore(Long id) {
        return platformStoreMapper.selectById(id);
    }

    @Override
    public PageResult<PlatformStoreDO> getPlatformStorePage(PlatformStorePageReqVO pageReqVO) {
        return platformStoreMapper.selectPage(pageReqVO);
    }

    @Override
    public List<PlatformStoreDO> getPlatformStoresByPlatformId(Long platformId) {
        return platformStoreMapper.selectListByPlatformId(platformId);
    }

    @Override
    public List<PlatformStoreDO> getAuthorizedStoresByPlatformId(Long platformId) {
        return platformStoreMapper.selectAuthorizedStoresByPlatformId(platformId);
    }

}